package com.xdf.docmanagementsystem.config;

import com.xdf.docmanagementsystem.common.util.EhCacheUtil;
import com.xdf.docmanagementsystem.common.util.JWTUtil;
import com.xdf.docmanagementsystem.entity.SysUser;
import com.xdf.docmanagementsystem.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @program: doc-management-system
 * @description: UserRealm
 * @author: Zhang
 * @create: 2020-06-19 16:48
 **/
@Configuration
public class UserRealm extends AuthorizingRealm {
    private SysUserService sysUserService;

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * Token验证
     *
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 获取授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = String.valueOf(principalCollection.getPrimaryPrincipal());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //  获取权限
        Set<String> set = new HashSet<String>();
        info.setStringPermissions(set);
        return info;
    }

    /**
     * 获取身份信息
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取当前Token
        String jwt = (String) authenticationToken.getPrincipal();
        SysUser user;
        String userName;
        try {
            userName = JWTUtil.getUserNameFromToken(jwt);
        } catch (Exception e) {
            throw new AuthenticationException("登录令牌无效!");
        }
        if (Objects.equals(jwt, EhCacheUtil.get(null, userName))) {
            return new SimpleAuthenticationInfo(jwt, jwt, this.getName());
        }
        try {
            user = sysUserService.selectByUserAccount(userName);
        } catch (Exception e) {
            throw new AuthenticationException(e.getMessage());
        }
        if (user == null)
            throw new AuthenticationException("无此用户!");
        if (user.getUserStatus() != 1)
            throw new AuthenticationException("该用户已停用!");
        if (!JWTUtil.verifyJWT(jwt, user.getUserAccount(), user.getUserPassword())) {
            throw new AuthenticationException("禁止登录!");
        }

        EhCacheUtil.put(null, user.getUserAccount(), jwt);

        return new SimpleAuthenticationInfo(jwt, jwt, this.getName());
    }

}
